package com.wmw.DataPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.wmw.Utility.Common;
import com.wmw.Utility.DBConstant;
import com.wmw.Utility.Dish;

public class OrdersData {
	public static final String _Dish = "Dish";

	public List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();
	public List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>();

	public void showItems(SQLiteDatabase db) {
		String col[] = { DBConstant._CTN_CLASSIFY_NAME,
				DBConstant._CTN_TOTAL_SALES };
		// 查询数据
		Cursor cur = db.query(DBConstant._ClassifyTableName, col, null, null,
				null, null, null);
		Integer num = cur.getCount();
		Log.d(Common.GetLineInfo(), "records number -" + num);

		for (int i = 0; i < num; i++) {
			if (cur.moveToFirst()) {// 判断游标是否为空
				cur.move(i);// 移动到指定记录
				String classifyStr = cur.getString(cur
						.getColumnIndex(DBConstant._CTN_CLASSIFY_NAME));
				this.addNewClassify(classifyStr, cur.getInt(cur
						.getColumnIndex(DBConstant._CTN_TOTAL_SALES)));

				Cursor subCur = db.query(DBConstant._DishTableName, null,
						DBConstant._DTN_BelongClassify + "==" + getClassifyID(db, classifyStr), null, null, null, null);
				int subNum = subCur.getCount();
				Log.d(Common.GetLineInfo(), "records number -" + subNum);

				List<Map<String, Object>> data = childData.get(i);
				for (int j = 0; j < subNum; ++j) {
					if (subCur.moveToFirst()) {// 判断游标是否为空
						subCur.move(j);// 移动到指定记录

						Map<String, Object> dish = createOneDish(
								subCur.getString(subCur
										.getColumnIndex(DBConstant._DTN_DishName)),
								subCur.getInt(subCur
										.getColumnIndex(DBConstant._DTN_TotalSales)),
								Dish.DishSalesState.values()[subCur.getInt(subCur
										.getColumnIndex(DBConstant._DTN_SaleState))],
								subCur.getFloat(subCur
										.getColumnIndex(DBConstant._DTN_Price)),
								subCur.getInt(subCur
										.getColumnIndex(DBConstant._DTN_PositiveCommentCnt)),
								subCur.getInt(subCur
										.getColumnIndex(DBConstant._DTN_ModerateCommentCnt)),
								subCur.getInt(subCur
										.getColumnIndex(DBConstant._DTN_NegativeCommentCnt)),
								subCur.getInt(subCur
										.getColumnIndex(DBConstant._DTN_LeftNum)));
						data.add(dish);
					}
				}
				subCur.close();
			}
		}

	}

	public int getClassifyID(SQLiteDatabase db, String classifyName) {
		String col[] = { DBConstant._CTN_CLASSIFY_ID };
		Cursor cur = db.query(DBConstant._ClassifyTableName, col,
				DBConstant._CTN_CLASSIFY_NAME + "=='" + classifyName + "'",
				null, null, null, null);
		Integer num = cur.getCount();
		Log.d(Common.GetLineInfo(), "records number -" + num);

		int id = -1;
		for (int i = 0; i < num; ++i) {
			if (cur.moveToFirst()) {// 判断游标是否为空
				cur.move(i);// 移动到指定记录
				id = cur.getInt(cur.getColumnIndex(DBConstant._CTN_CLASSIFY_ID));
				break;
			}
		}
		cur.close();

		return id;
	}
	
	public boolean isDishInDB(SQLiteDatabase db, String dishName) {
		String col[] = { DBConstant._DTN_DishName };
		String[] selectionArgs = { dishName };
		Cursor cur = db.query(DBConstant._DishTableName, col,
				DBConstant._DTN_DishName + "==?", selectionArgs, null, null,
				null);
		return cur.getCount() != 0;
	}
	public int getDishID(SQLiteDatabase db, String dishName) {
		String col[] = { DBConstant._DTN_DishID };
		Cursor cur = db.query(DBConstant._DishTableName, col,
				DBConstant._DTN_DishName + "=='" + dishName + "'",
				null, null, null, null);
		Integer num = cur.getCount();
		Log.d(Common.GetLineInfo(), "records number -" + num);

		int id = -1;
		for (int i = 0; i < num; ++i) {
			if (cur.moveToFirst()) {// 判断游标是否为空
				cur.move(i);// 移动到指定记录
				id = cur.getInt(cur.getColumnIndex(DBConstant._DTN_DishID));
				break;
			}
		}
		cur.close();

		return id;
	}
	public Map<String, Object> createOneDish(String dishName, int totalSales,
			Dish.DishSalesState salesState, float price,
			int positiveCommentsCnt, int moderateCommentsCnt,
			int negativeCommentsCnt, int leftNum) {
		Log.d(Common.GetLineInfo(), "Creating one dish");
		Map<String, Object> curChildMap = new HashMap<String, Object>();
		Dish dish = new Dish(dishName, totalSales, salesState, price,
				positiveCommentsCnt, moderateCommentsCnt, negativeCommentsCnt,
				leftNum);
		curChildMap.put(_Dish, dish);
		return curChildMap;
	}

	public void addNewClassify() {
		addNewClassify(
				"新分类(如十元特色菜)"
						+ new Random(System.currentTimeMillis()).nextInt(), 0);
	}

	public void addNewClassify(String classifyName, int salesNum) {
		Map<String, Object> curGroupMap = new HashMap<String, Object>();
		groupData.add(curGroupMap);
		curGroupMap.put(DBConstant._CTN_CLASSIFY_NAME, classifyName);
		curGroupMap.put(DBConstant._CTN_TOTAL_SALES, salesNum);

		List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
		childData.add(children);
	}

}
